package com.code.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ThreadJoin {

    public static void main(String[] args) {
//        method1();
        method2();
    }

    public  static void method2() {
        CountDownLatch countDownLatch = new CountDownLatch(2);
        Thread t1 = new Thread() {
            @Override
            public  void run() {
                System.out.println("111");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                countDownLatch.countDown();
            }
        };

        Thread t2 = new Thread() {
            @Override
            public  void run() {
                System.out.println("111");
                countDownLatch.countDown();
            }
        };

        try {
            t1.start();
            t2.start();
            countDownLatch.await();
            System.out.println("end ======================");
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }


    }
    public static void method1() {

        Thread t1 = new Thread() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getId() + ", " +Thread.currentThread().getName());
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getId() + ", " +Thread.currentThread().getName());
            }
        };
        try {
            t1.start();
            t2.start();
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("all over");

    }
}
